<!--
 * @Description:
 * @Version: 1.0
 * @Author: liguansheng
 * @Date: 2022-08-23 13:32:55
 * @LastEditors: liguansheng
 * @LastEditTime: 2022-08-23 17:33:01
-->
<template>
  <el-form
    :model="form"
    :rules="rules"
    label-width="120px"
    label-position="top"
  >
    <el-form-item
      label="名称"
      prop="name"
    >
      <el-input v-model="form.name" />
    </el-form-item>
    <el-form-item label="显示名称">
      <el-input v-model="form.displayName" />
    </el-form-item>
    <el-form-item label="描述">
      <el-input v-model="form.description" />
    </el-form-item>
    <el-form-item label="">
      <el-checkbox
        v-model="form.enabled"
        label="启用"
      />
    </el-form-item>
    <el-form-item label="">
      <el-checkbox
        v-model="form.required"
        label="必要"
      />
    </el-form-item>
    <el-form-item label="">
      <el-checkbox
        v-model="form.emphasize"
        label="强调"
      />
    </el-form-item>
    <el-form-item label="">
      <el-checkbox
        v-model="form.showInDiscoveryDocument"
        label="在发现文档中显示"
      />
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { PropType, ref, watch } from 'vue'
import type { IElFormRule } from '@/types/element-plus'

const props = defineProps({
  modelValue: {
    type: Object as PropType<{
      name: string
      displayName: string
      description: string
      enabled: boolean
      required: boolean
      emphasize: boolean
      showInDiscoveryDocument: boolean
    }>,
    required: true
  }
})
interface EmitsType {
  (e: 'update:modelValue', form: {
      name: string
      displayName: string
      description: string
      enabled: boolean
      required: boolean
      emphasize: boolean
      showInDiscoveryDocument: boolean
    }): void
}
const emit = defineEmits<EmitsType>()

const form = ref(props.modelValue)
const rules = ref<IElFormRule>({
  name: [
    { required: true, message: '名称不能为空', trigger: 'blur' }
  ]
})

watch(() => props.modelValue, () => {
  form.value = props.modelValue
}, {
  deep: true
})

watch(form.value, () => {
  emit('update:modelValue', form.value)
})
</script>

<style scoped lang="scss">

</style>
